import React from 'react';
import { useDrsState } from 'umi';
import { EDrsBtnDisableType } from "../../enums/userInfo";
/**
 * 按钮权限，包裹的节点需要暴露disabled属性
 * - 有权限：`return children`
 * - 无权限（不展示）：`return noPermissionRender ?? null`
 * - 无权限（禁用）：为当前节点加上disabled=true属性
 */
export default function PermissionWrapper(_ref) {
  var _getBtnInfo;
  var code = _ref.code,
    children = _ref.children,
    noPermissionRender = _ref.noPermissionRender;
  var _useDrsState = useDrsState(),
    getBtnInfo = _useDrsState.getBtnInfo;
  var _ref2 = (_getBtnInfo = getBtnInfo(code)) !== null && _getBtnInfo !== void 0 ? _getBtnInfo : {},
    disableType = _ref2.disableType,
    roleBtnFlag = _ref2.roleBtnFlag;
  if (roleBtnFlag) return children;
  if (! /*#__PURE__*/React.isValidElement(children)) return children;
  if ((disableType === null || disableType === void 0 ? void 0 : disableType.code) === EDrsBtnDisableType.ASH) {
    return /*#__PURE__*/React.cloneElement(children, {
      disabled: true
    });
  }
  return noPermissionRender !== null && noPermissionRender !== void 0 ? noPermissionRender : null;
}